import {
  Component,
  Injector,
  ViewChild,
  ViewEncapsulation
} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { MaterialChargeServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';

@Component({
  templateUrl: './log.component.html',
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class MaterialLogComponent extends AppComponentBase {
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  advancedFiltersAreShown = false;
  filterText = '';
  TaskCode = '';
  DrawingCode = '';
  MaterialName = '';
  MaterialStatus = '';
  InvFurBatch = '';
  Hgz_zjd = '';
  InvCode = '';
  PaiHao = '';
  MaterialSpec = '';
  Hgz_zyd = '';
  MaterialSize = '';
  InvMeasName = '';
  TechCondition = '';
  InvDepMeasName = '';
  HgzNo = '';
  tab = 1;

  constructor(
    injector: Injector,
    private _materialServiceProxy: MaterialChargeServiceProxy
  ) {
    super(injector);
  }

  getLogs(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._materialServiceProxy
      .getAllMaterialLogs(
        undefined,
        this.filterText,
        this.TaskCode,
        this.DrawingCode,
        this.MaterialName,
        this.PaiHao,
        this.MaterialSpec,
        this.HgzNo,
        this.Hgz_zjd,
        this.Hgz_zyd,
        this.InvCode,
        this.InvFurBatch,
        this.InvMeasName,
        this.InvDepMeasName,
        this.MaterialSize,
        this.MaterialStatus,
        this.TechCondition,
        this.tab,
        this.primengTableHelper.getSorting(this.dataTable),
        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        console.log(result);
        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  tabChange(tab: number) {
    this.tab = tab;
    this.reloadPage();
  }
}
